package com.dropgame.tools;

import java.lang.reflect.Array;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;


public class ImageButton {

	private final Sprite[] keyFrames;
	private final float frameDuration;
	private final float animationDuration;
	private Input input;
	private OrthographicCamera guiCam;
	public Rectangle viewport;
	private Boolean touched = false;
	private Boolean completed = false;
	private Rectangle bounds;
	private Boolean  acceptInput = true;
	private Boolean hasCamera;
	private int frameNumber;
	private float stateTime;
	public float xPos;
	public float yPos;
	private float scale;
	
	public ImageButton(float frameDuration, Sprite[] keyFrames){
		this.frameDuration = frameDuration;
		this.animationDuration = keyFrames.length * frameDuration;
		this.keyFrames = new Sprite[keyFrames.length];
		for(int i = 0; i < keyFrames.length; i++){
			this.keyFrames[i] = keyFrames[i];
		}
		this.bounds = keyFrames[0].getBoundingRectangle();
	}
	
	// Constructor for imageButton that creates an resolution dependent touchable button.
	public ImageButton(float frameDuration, Sprite[] keyFrames, Input input, OrthographicCamera guiCam, Rectangle viewport, float xPos, float yPos){
		frameNumber = 0;
		this.frameDuration = frameDuration;
		this.animationDuration = keyFrames.length * frameDuration;
		this.keyFrames = new Sprite[keyFrames.length];
		this.setxPos(xPos);
		this.setyPos(yPos);
		for(int i = 0; i < keyFrames.length; i++){
			this.keyFrames[i] = keyFrames[i];
			this.keyFrames[i].setPosition(this.getxPos(), this.getyPos());
		}
		this.input = input;
		this.guiCam = guiCam;
		this.viewport = viewport;
		this.bounds = keyFrames[0].getBoundingRectangle();
		
		
	}
	
	
	// Returns the keyFrame depending on the time since the button was touched.
	public Sprite getKeyFrame(float deltaTime){
		setCompleted(false);
		if(input.justTouched()){
			Vector3 worldCoordinates = new Vector3(input.getX(), input.getY(), 0);
			guiCam.unproject(worldCoordinates, viewport.x, viewport.y, viewport.width, viewport.height);
			if(bounds.contains(worldCoordinates.x, worldCoordinates.y) && getAcceptInput()){
				setTouched(true);
			}
		}
		
		if(getTouched()){
			stateTime += deltaTime;
			frameNumber = (int)(stateTime/frameDuration);
			//Regular
			//frameNumber = Math.min(keyFrames.length -1, frameNumber);
			//Looping
			frameNumber = frameNumber % (keyFrames.length * 2);
            if (frameNumber >= keyFrames.length) frameNumber = keyFrames.length - 1 - (frameNumber - keyFrames.length);

		}
		
		// If the in and out animation has completed.
		if(stateTime > animationDuration*2){
			setTouched(false);
			setCompleted(true);
			stateTime = 0;
		}
		
		
		bounds = keyFrames[frameNumber].getBoundingRectangle();
		keyFrames[frameNumber].setPosition(getxPos(), getyPos());
		//keyFrames[frameNumber].setScale(scale);
		return keyFrames[frameNumber];
		
	}

	public Boolean getTouched() {
		return touched;
	}

	public void setTouched(Boolean touched) {
		this.touched = touched;
	}

	public Boolean getCompleted() {
		return completed;
	}

	public void setCompleted(Boolean completed) {
		this.completed = completed;
	}

	public Boolean getAcceptInput() {
		return acceptInput;
	}

	public void setAcceptInput(Boolean acceptInput) {
		this.acceptInput = acceptInput;
	}
	
	

	public float getxPos() {
		return xPos;
	}

	public void setxPos(float xPos) {
		this.xPos = xPos;
	}

	public float getyPos() {
		return yPos;
	}

	public void setyPos(float yPos) {
		this.yPos = yPos;
	}
	
}
